package com.example.sfsmaintainer

import android.content.Context
import android.os.Bundle
import android.os.PowerManager
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import com.baidu.mapapi.SDKInitializer

class MainActivity: FlutterActivity() {
    private val CHANNEL = "wake_up_screen"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        SDKInitializer.setAgreePrivacy(application, true)
        SDKInitializer.initialize(application)
    }

    // 注册 MethodChannel
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
                call, result ->
            if (call.method == "wakeUp") {
                wakeUpScreen()
                result.success("done")
            } else {
                result.notImplemented()
            }
        }
    }

    private fun wakeUpScreen() {
        val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
        val isScreenOn = powerManager.isInteractive

        if (!isScreenOn) {
            val wakeLock = powerManager.newWakeLock(
                PowerManager.SCREEN_BRIGHT_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP,
                "sfsmaintainer:WakeLock"
            )
            wakeLock.acquire(3000) // 点亮屏幕 3 秒
        }
    }
}



